﻿<?php
session_start();

//Pear & Propel Stuffs
set_include_path(get_include_path() .PATH_SEPARATOR. "libs/pear");
set_include_path(get_include_path() .PATH_SEPARATOR. "libs/propel/runtime/lib");
set_include_path(get_include_path() .PATH_SEPARATOR. "bdd/build/classes");
require_once 'libs/propel/runtime/lib/Propel.php';
Propel::init("bdd/build/conf/tshirtfactory-conf.php");

//Smarty stuffs
define('SMARTY_DIR', 'libs/');
require(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = 'pages/templates/';
$smarty->compile_dir = 'pages/templates_c/';
$smarty->config_dir = 'pages/configs/';
$smarty->cache_dir = 'pages/cache/';

//Personnal stuffs
include "config.php";
include "pages/classes/Connexion.php";
include "pages/classes/ArticleCart.php";

//Logout
if(isset($_GET['logout']))
{
	unset($_SESSION['connexion']);
}
//empty cart
if(isset($_GET['cart']) && $_GET['cart'] == 'empty')
{
	$_SESSION['cart'] = array();
}

//Connection
$page="home";
$connexionFailed = false;
if(isset($_POST['connection']))
{
	$page = $_POST['page'];
	if(!Connexion::createConnexion($_POST['mail'], $_POST['password']))
	{
		$connexionFailed = true;
	}
}
else
{
	if(isset($_GET['page'])){
		$page = $_GET['page'];
	}
}

//Cart
if(!isset($_SESSION['cart']))
{
	$_SESSION['cart'] = array();	//Unset the cart when the purchase is done.
}

//Smarty initialization
$smarty->assign('nbArticles', count($_SESSION['cart']));
$smarty->assign('connected', isset($_SESSION['connexion']));

//Controller
if($connexionFailed)
{
	include "pages/controller/connection.php";
}
else if (!isset($page) || $page == "home") {
    include "pages/controller/home.php";
} else if ($page == "store") {
    include "pages/controller/store.php";
} else if ($page == "search") {
    include "pages/controller/search.php";
} else if ($page == "cart") {
    include "pages/controller/cart.php";
} else if ($page == "info") {
    include "pages/controller/info.php";
} else if ($page == "account_registration") {
    include "pages/controller/account_edit.php";
} else if ($page == "account_registration_recap") {
    include "pages/controller/account_registration_recap.php";
} else if ($page == "account_checker") {
    include "pages/controller/account_checker.php";
}
//Pages that need a connection
else if ($page == "account") {
    include checkConnection("pages/controller/account.php");
} else if ($page == "account_edit") {
    include checkConnection("pages/controller/account_edit.php");
} else if ($page == "validation") {
    include checkConnection("pages/controller/cart_validation.php");
} else if ($page == "payement") {
    include checkConnection("pages/controller/payement.php");
} else if ($page == "account_deleted") {
    include checkConnection("pages/controller/account_deleted.php");
}

//Function that display the wanted page or the connection page depending on the connection state.
function checkConnection($page)
{
	if(!isset($_SESSION['connexion']))
	{
		return "pages/controller/connection.php";
	}
	else
	{
		return $page;
	}
}